export type CallerFunc<T> = () => Promise<T>


/**
 * Promise错误重试
 * @param caller
 * @param count 尝试的次数
 * @constructor
 */
export function PromiseErrorTryAgain<T>(caller: CallerFunc<T>, count: number = 3): Promise<T> {
  return new Promise<T>((resolve, reject) => {
    runPromise(caller, count - 1, resolve, reject)
  })
}

function runPromise<T>(caller: CallerFunc<T>, count: number, resolve, reject) {
  caller().then(resolve).catch((...err) => {
    if (0 < count) {
      runPromise(caller, count - 1, resolve, reject)
    } else {
      reject(...err)
    }
  })
}

/**

 PromiseErrorTryAgain(()=>Promise.reject(),3)

 */
